package com.colin.app.activity.senior.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;

import com.colin.app.MyAIDLService;
import com.colin.app.R;
import com.colin.app.activity.senior.SeniorContract;
import com.colin.app.activity.senior.SeniorPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.app.service.DemoService;
import com.colin.app.service.RemoteService;
import com.colin.library.help.LogHelp;

public class ServiceActivity extends AppActivity implements SeniorContract.SeniorView, View.OnClickListener {
    private SeniorContract.SeniorPresenter mSeniorPresenter = null;
    private DemoService.DemoBinder demoBinder;
    private MyAIDLService myAIDLService;

    @Override
    protected void onDestroy() {
        if (null != demoBinder) {
            demoBinder = null;
        }
        if (null != remote_connection && null != myAIDLService) {
            try {
                unbindService(remote_connection);
                myAIDLService = null;
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }

        if (null != mSeniorPresenter) {
            mSeniorPresenter.destroy();
            mSeniorPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mSeniorPresenter = new SeniorPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_service;
    }

    @Override
    public void initView() {
        LogHelp.d("ServiceActivity thread id is " + Thread.currentThread().getName());
    }

    @Override
    public void initData() {
        mSeniorPresenter.setData();
    }

    @Override
    public void initListener() {
        findViewById(R.id.button_service_start).setOnClickListener(this);
        findViewById(R.id.button_service_stop).setOnClickListener(this);
        findViewById(R.id.button_service_bind).setOnClickListener(this);
        findViewById(R.id.button_service_unbind).setOnClickListener(this);
        findViewById(R.id.button_service_bind_remote).setOnClickListener(this);
        findViewById(R.id.button_service_unbind_remote).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.button_service_start:
                intent = new Intent(this, DemoService.class);
                startService(intent);
                break;
//            一个Service必须要在既没有和任何Activity关联又处理停止状态的时候才会被销毁。
            case R.id.button_service_stop:
                intent = new Intent(this, DemoService.class);
                stopService(intent);
                break;
            case R.id.button_service_bind:
                intent = new Intent(this, DemoService.class);
                bindService(intent, connection, BIND_AUTO_CREATE);
                break;
//            一个Service必须要在既没有和任何Activity关联又处理停止状态的时候才会被销毁。
            case R.id.button_service_unbind:
                try {
                    unbindService(connection);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button_service_bind_remote:
                intent = new Intent(this, RemoteService.class);
                bindService(intent, remote_connection, BIND_AUTO_CREATE);
                break;
//            一个Service必须要在既没有和任何Activity关联又处理停止状态的时候才会被销毁。
            case R.id.button_service_unbind_remote:
                try {
                    unbindService(remote_connection);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogHelp.d("onServiceDisconnected==" + name);
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogHelp.d("onServiceConnected==" + name);
            demoBinder = (DemoService.DemoBinder) service;
            demoBinder.startDownload();
        }
    };

    private ServiceConnection remote_connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogHelp.d("onServiceDisconnected==" + name);
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogHelp.d("onServiceConnected==" + name);
            LogHelp.d("Service thread id is " + Thread.currentThread().getName());
            myAIDLService = MyAIDLService.Stub.asInterface(service);
            try {
                int result = myAIDLService.plus(3, 5);
                String upperStr = myAIDLService.toUpperCase("hello world");
                LogHelp.d("TAG", "result is " + result);
                LogHelp.d("TAG", "upperStr is " + upperStr);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
}
